#include"LinkStack.h"
#include"LinkQueue.h"
#include"MyString.h"
#include<stdlib.h>
#include<stdio.h>

#define MAXNum 3
struct Car
{
    MyString name;
    double arriveTime;
    double leaveTime;
};

struct Car *CreateCar(const char *name,double arriveTime)
{
    struct Car* newCar=(struct Car*)malloc(sizeof(struct Car));
    if(newCar==NULL)
    {
        printf("newCar malloc error!\n");
        return NULL;
    }
    StringInitialize(&newCar->name,name);
    newCar->arriveTime=arriveTime;
    newCar->leaveTime=0;
}

void FreeCar(struct Car* leaveCar)
{
    if(leaveCar==NULL)
        return;
    FreeString(&leaveCar->name);
    free(leaveCar);
}

void Come_Car(LStack *CarStop,LQueue *pavement)
{
    char carName[100]={0};
    double arriveTime=0;
    printf("please input your car name:");
    scanf("%s",carName);
    printf("please input your car arriveTime:");
    scanf("%lf",&arriveTime);
    struct Car* newCar=CreateCar(carName,arriveTime);
    if(newCar==NULL)
    {
        printf("newCar malloc error!\n");
        return;
    }

    if(CarStop->stack.len>=MAXNum)
    {
        QPush(pavement,newCar);
        printf("停车场已满,汽车|%s|停在了便道的%d个位置\n",newCar->name.string,pavement->queue.len);
    }
    else
    {
        Push(CarStop,newCar);
        printf("停车场未满,汽车|%s|停在了停车场的%d个位置\n",newCar->name.string,CarStop->stack.len);
    }
}

void Leave_Car(LStack *CarStop,LQueue *pavement)
{
    printf("请输入你要出停车场的车牌:");
    char temp[100]={0};
    scanf("%s",temp);
    Init_MyString(CarName,temp);

    LStack TempStop;
    InitLinkStack(&TempStop);

    int Num=CarStop->stack.len;
    double LeaveCarTime=0;

    while(IsEmpty(CarStop)==false)
    {
        struct Car* car=(struct Car*)*Pop(CarStop);
        if(car->name.isEqual(&car->name,&CarName)==true)
        {
            printf("请输入车辆离开时间");
            scanf("%lf",&car->leaveTime);
            printf("车辆|%s|在%lf时间到达停车场,在%lf时间离开停车场,停车费用是%lf\n",
            car->name.string,car->arriveTime,car->leaveTime,
            (car->leaveTime-car->arriveTime)*5);
            FreeCar(car);
            break;
        }
        else
        {
            Push(&TempStop,car);
        }
    }
    while(IsEmpty(&TempStop)==false)
    {
        Push(CarStop,*Pop(&TempStop));
    }
    if(Num==CarStop->stack.len)
    {
        printf("未找到你要的车牌!\n");
    }
    else
    {
        if(IsQEmpty(pavement)==false)
        {
            struct Car* car=(struct Car*)*QPop(pavement);
            car->arriveTime=LeaveCarTime;
            Push(CarStop,car);
            printf("车辆|%s|在%lf时间停在了停车场的第%d个位置\n",
            car->name.string,car->arriveTime,pavement->queue.len);
        }
    }
}

void DisplayStack(LStack *s)
{
    struct Node *TravelPoint=s->stack.head;
    while(TravelPoint!=NULL)
    {
        struct Car* car=(struct Car*)TravelPoint->data;
        printf("车牌:%s  到达时间:%lf\n",car->name.string,car->arriveTime);
        TravelPoint=TravelPoint->next;
    }
    printf("\n");
}

void DisplayQueue(LQueue *s)
{
    struct Node *TravelPoint=s->queue.head;
    while(TravelPoint!=NULL)
    {
        struct Car* car=(struct Car*)TravelPoint->data;
        printf("车牌:%s  到达时间:%lf\n",car->name.string,car->arriveTime);
        TravelPoint=TravelPoint->next;
    }
    printf("\n");
}

int main()
{
    LStack carStop;
    InitLinkStack(&carStop);
    LQueue pavement;
    InitLQueue(&pavement);

    int choice=0;
    while(1)
    {
        printf("**********************CarStop***********************\n");
        printf("1.车辆进入停车场\n");
        printf("2.车辆离开停车场\n");
        printf("3.查询停车场状态\n");
        printf("4.查询便道状态  \n");
        printf("5.退出停车场系统\n");
        printf("请输入你的选择:");

        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                Come_Car(&carStop,&pavement);
                break;
            case 2:
                Leave_Car(&carStop,&pavement);
                break;
            case 3:
                DisplayStack(&carStop);
                break;
            case 4:
                DisplayQueue(&pavement);
                break;
            case 5:
                printf("thanks for using!\n");
                struct Node *TravelPoint=carStop.stack.head;
                while(TravelPoint!=NULL)
                {
                    struct Car* car=(struct Car*)TravelPoint->data;
                    FreeCar(car);
                    TravelPoint=TravelPoint->next;
                }
                FreeStack(&carStop);

                TravelPoint=pavement.queue.head;
                while(TravelPoint!=NULL)
                {
                    struct Car* car=(struct Car*)TravelPoint->data;
                    FreeCar(car);
                    TravelPoint=TravelPoint->next;
                }
                FreeLQueue(&pavement);
                return 0;
        }
    }
}